import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const ControlModalPrice	= $Lazy.state(()=>import('./cpt/control-modal-price'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '状态',		name: 'enabled',	type: 'select', loading:true },
			{ label: '物价代码',		name: 'price_code' },
			{ label: '项目名称',		name: 'item_name' },
			{ label: '物价分类',		name: 'item_attr' },
			{ label: '助记码',		name: 'qt_item_code' },
			{ label: '类型',  		name: 'type', type:'select', nameStr:'name',data:[{name:'源物价',value:'1'},{name:'病理',value:'3'}] },
		],
		// 提交表单列表
		formData : [
			{ label: '物价代码',			name: 'price_code', 		half:true, 	required:true, disabled:true },
			{ label: '项目名称',			name: 'item_name', 			half:true, 	required:true },
			{ label: '物价分类',			name: 'dist_code', 			half:true, 	type:'select', idStr:'code' },
			{ label: '官方物价代码',		name: 'source_code', 		half:true },
			{ label: '普通价格',			name: 'price', 				half:true, 	required:true, btn: {label: '同步', onClick: value => this.formRef.setValue({ hosp_price_ta: value, hosp_price_tb: value, hosp_price_sa: value, hosp_price_sb: value })} },
			{ label: '三甲医院价格',		name: 'hosp_price_ta', 		half:true, 	required:true },
			{ label: '三乙医院价格',		name: 'hosp_price_tb', 		half:true, 	required:true  },
			{ label: '二甲医院价格',		name: 'hosp_price_sa', 		half:true, 	required:true  },
			{ label: '二乙医院价格',		name: 'hosp_price_sb', 		half:true, 	required:true  },
			{ label: '检测方法',			name: 'detection_method', 	half:true, 	required:true, 	type:'select', idStr:'code',nameStr:'name'  },
			{ label: '计价单位',			name: 'price_unit', 		half:true },
			{ label: '助记码',			name: 'qt_item_code', 		half:true },
			{ label: '物价状态',			name: 'enabled', 			half:true, 	required:true, 	type:'select', nameStr:'name',data:[{name:'启用',value:'1'},{name:'禁用',value:'-1'}] },
			{ label: '排序',			name: 'sort', 				half:true, 	type:'number' },
			{ label: '项目内涵',     	name: 'extend1', 			full:true, 	type:'textarea' },
			{ label: '除外内容',     	name: 'extend2', 			full:true, 	type:'textarea' },
			{ label: '备注',     		name: 'remark', 			full:true, 	type:'textarea' }
		]
	}
	
	model = {}
	query = {type:1}
	
	componentDidMount(){
		this.getSelect()
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 状态
			$fn.setRow(forms, 'enabled',  await f.company_state)
			
			this.setState({ forms })
		})
	}

	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, ['detection_method','dist_code'], ()=>{
			$cache.then( async f => {
				// 检测方法
				$fn.setRow(formData, 'detection_method',  await f.getDicCache({ code: 16000}))
				// 物价分类
				$fn.setRow(formData, 'dist_code', await f.getDicCache({ code: 89000 }))
				//
				this.setState({ formData })
			})
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'qt-item-source/index')
	
	// 表单
	cols = [
		{ title: '物价代码',			dataIndex: 'price_code', 				width:150 },
		{ title: '官方物价代码', 	dataIndex: 'source_code', 				width:150 },
		// { title: '关联物价', 		dataIndex: 'price_unit', 				width:150 },
		{ title: '项目名称', 		dataIndex: 'item_name', 				width:200 },
		{ title: '物价分类', 		dataIndex: 'attr', 						width:100 },
		{ title: '检测方法', 		dataIndex: 'detection_method_name', 	width:200 },
		{ title: '普通价格',			dataIndex: 'price', 					...$fn.table.num },
		{ title: '三甲价格', 		dataIndex: 'hosp_price_ta', 			...$fn.table.num },
		{ title: '三乙价格', 		dataIndex: 'hosp_price_tb', 			...$fn.table.num },
		{ title: '二甲价格', 		dataIndex: 'hosp_price_sa', 			...$fn.table.num },
		{ title: '二乙价格', 		dataIndex: 'hosp_price_sb', 			...$fn.table.num },
		{ title: '项目内涵', 		dataIndex: 'extend1', 					width:150 },
		{ title: '除外内容', 		dataIndex: 'extend2', 					width:150 },
		{ title: '助记码', 			dataIndex: 'qt_item_code', 				width:100 },
		{ title: '计价单位', 		dataIndex: 'price_unit', 				width:100 },
		{ title: '备注', 			dataIndex: 'remark', 					width:150 },
		{ title: '状态',			dataIndex: 'enabled', 					...$fn.table.status, render: text => +text === 1 ? '启用' : '禁用' },
		{ title: '最后一次修改人', 	dataIndex: 'update_user', 				width:100 },
		{ title: '最后一次修改时间', dataIndex: 'updated_at', 				...$fn.table.time },
		TableControls.controls.call(this,{ width: 70 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'qt-item-source/info', { uuid } ,{
				select: this.getModalSelect,
				click: () => $fn.hideRows(this.state.formData, ['price_code'], false)
			}),
		]
	}
	// 操作按钮
	controls = () => {
		const {selectedRows} = this.state
		return [
			// 添加
			Controls.add.call(this, { click: ()=>{
				const { formData } = this.state
				$fn.hideRows(formData, ['price_code'], true)
				this.getModalSelect()
				this.setState({ formData })
			} }),
			// 文件导入
			Controls.imports.call(this,'qt-item-source/import', {op_key: 'QtItemSourceExcel', reconfirm: true}),
			// 文件导出
			Controls.exports.call(this, 'qt-item-source/index'),
			// 下载导入模板
			Controls.importTemplate.call(this),
			// 设置物价分类
			Controls.click.call(this, '设置物价分类', ()=>this.modalprice.open(selectedRows), {isKeys:true} ),
		]
	}
	
	render(){
		return (
			<>
				<Page title='源物价项目' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s8', width:900, api: 'qt-item-source/add', editApi:'qt-item-source/edit'  }) }
				{/* 设置物价分类 */}
				<ControlModalPrice onRef={ref=>this.modalprice=ref} onRefresh={this.fetch} />
			</>
		)
	}
}

export default Index